package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcCurTaskQueryGroupMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcCurTaskQuery;
import com.csun.cmny.provider.model.domain.OmcCurTaskQueryGroup;
import com.csun.cmny.provider.model.dto.OmcCurTaskQueryDto;
import com.csun.cmny.provider.model.vo.query.CurTaskQueryVo;
import com.csun.cmny.provider.service.OmcCurTaskQueryGroupService;
import com.csun.cmny.provider.service.OmcCurTaskQueryService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-12 13:27
 * @Description:
 */
@Service
@Transactional
public class OmcCurTaskQueryGroupServiceImpl extends BaseService<OmcCurTaskQueryGroup>
        implements OmcCurTaskQueryGroupService {

    @Resource
    private OmcCurTaskQueryService omcCurTaskQueryService;
    @Resource
    private OmcCurTaskQueryGroupMapper omcCurTaskQueryGroupMapper;

    /**
     * query cur task query group list with page
     *
     * @param omcCurTaskQueryDto the omc cur task query dto
     *
     * @return the list.
     */
    @Override
    public List<CurTaskQueryVo> queryCurTaskQueryGroupListWithPage(OmcCurTaskQueryDto omcCurTaskQueryDto) {

        return omcCurTaskQueryGroupMapper.queryCurTaskQueryGroupListWithPage(omcCurTaskQueryDto);
    }

    /**
     * query cur task query by code and  group id
     *
     * @param code    the query code
     * @param groupId the group id
     * @return the cur task query vo
     */
    @Override
    public CurTaskQueryVo queryCurTaskQueryGroupByCodeAndGroupId(String code, Long groupId) {

        OmcCurTaskQueryDto omcCurTaskQueryDto = new OmcCurTaskQueryDto();
        omcCurTaskQueryDto.setGroupId(groupId);
        omcCurTaskQueryDto.setCode(code);

        PageHelper.startPage(1, 1);

        List<CurTaskQueryVo> curTaskQueryVoList = queryCurTaskQueryGroupListWithPage(omcCurTaskQueryDto);
        if (PublicUtil.isEmpty(curTaskQueryVoList)) {
            return null;
        }

        return curTaskQueryVoList.get(0);
    }

    /**
     * bind cur task query id and group id
     *
     * @param omcCurTaskQueryGroup the omc cur task query group
     *
     * @return the int.
     */
    @Override
    public Integer bind(OmcCurTaskQueryGroup omcCurTaskQueryGroup) {

        // 校验curTaskQueryId是否存在
        OmcCurTaskQuery omcCurTaskQuery = omcCurTaskQueryService.selectByKey(omcCurTaskQueryGroup.getCurTaskQueryId());

        if (PublicUtil.isEmpty(omcCurTaskQuery)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031301);
        }

        // TODO 校验是否绑定过，暂不需要

        return omcCurTaskQueryGroupMapper.insert(omcCurTaskQueryGroup);
    }

    /**
     * untied cur task query id and group id
     *
     * @param omcCurTaskQueryGroup the omc cur task query group
     *
     * @return the int.
     */
    @Override
    public Integer untied(OmcCurTaskQueryGroup omcCurTaskQueryGroup) {

        return omcCurTaskQueryGroupMapper.delete(omcCurTaskQueryGroup);
    }
}
